package com.kyxx.topics.fragment;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.kyxx.topics.R;
import com.kyxx.topics.adapter.TabFragmentAdapter;
import com.nostra13.universalimageloader.core.DisplayImageOptions;

import java.util.ArrayList;
import java.util.List;

import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;

public class LiveFragment extends Fragment {
    private View view;
    private DisplayImageOptions options;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private String[] titles = new String[]{"关注", "热门", "附近"};

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_live, container, false);

        options = new DisplayImageOptions.Builder()
                // 设置图片在下载期间显示的图片
                .showImageOnLoading(R.mipmap.icon_user_default)
                // 设置图片Uri为空或是错误的时候显示的图片
                .showImageForEmptyUri(R.mipmap.icon_user_default)
                // 设置图片加载/解码过程中错误时候显示的图片
                .showImageOnFail(R.mipmap.icon_user_default)
                .cacheInMemory(true)
                // 设置下载的图片是否缓存在内存中
                .cacheOnDisk(true)
                // 设置下载的图片是否缓存在SD卡中
                .bitmapConfig(Bitmap.Config.RGB_565)// 设置图片的解码类型
//                .displayer(new RoundedBitmapDisplayer(9999))
                .build();

        initView();
        initListener();

        return view;
    }

    /**
     * 初始化控件
     */
    private void initView() {
        tabLayout = (TabLayout) view.findViewById(R.id.tab_layout);
        viewPager = (ViewPager) view.findViewById(R.id.viewpager);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                JCVideoPlayer.releaseAllVideos();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        initFragment();
    }

    private void initFragment() {
        List<Fragment> fragments = new ArrayList<Fragment>();
        fragments.add(new LiveListFragment());
        fragments.add(new LiveListFragment());
        fragments.add(new LiveListFragment());
        viewPager.setAdapter(new TabFragmentAdapter(fragments, titles, getChildFragmentManager(), getActivity()));
        tabLayout.setupWithViewPager(viewPager);
    }

    /**
     * 绑定监听事件
     */
    private void initListener() {
    }
}
